package com.bootdo.wx.controller;

import java.util.List;
import java.util.Map;
import java.util.Date;
import java.util.HashMap;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.wx.domain.LessonClassDO;
import com.bootdo.wx.domain.LessonDO;
import com.bootdo.wx.service.GoodsGoodsService;
import com.bootdo.wx.service.LessonClassService;
import com.bootdo.wx.service.LessonService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.UUIDUtils;

/**
 * 
 * 
 * @author long
 * @email 499626561@qq.com
 * @date 2020-06-09 16:06:40
 */
 
@Controller
@RequestMapping("/wx/lesson")
public class LessonController {
	@Autowired
	private LessonService lessonService;
	@Autowired
	private LessonClassService lessonClassService;
	@Autowired
	private GoodsGoodsService goodsGoodsService;
	
	@GetMapping()
	@RequiresPermissions("wx:lesson:lesson")
	String Lesson(){
	    return "wx/lesson/lesson";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("wx:lesson:lesson")
	public PageUtils list(@RequestParam Map<String, Object> params){
	    params.put("delFlag","0");
	    params.put("sort","localtion");
	    params.put("order","asc");
		//查询列表数据
        Query query = new Query(params);
        if(params.containsKey("name")&&params.get("name")!=null&&!"".equals(params.get("name").toString())){
        	List<LessonDO> lessonList = lessonService.likeList(query);
        	if(lessonList.size()>0){
        		for (LessonDO lessonDO : lessonList) {
        			LessonClassDO lessonClassDO = lessonClassService.get(lessonDO.getClassId());
        			if(lessonClassDO!=null){
        				lessonDO.setClassId(lessonClassDO.getName());
        			}else{
        				lessonDO.setClassId("");
        			}
				}
        	}
        	int total =lessonList.size();
    		PageUtils pageUtils = new PageUtils(lessonList, total);
    		return pageUtils;
        }
		List<LessonDO> lessonList = lessonService.list(query);
		if(lessonList.size()>0){
    		for (LessonDO lessonDO : lessonList) {
    			LessonClassDO lessonClassDO = lessonClassService.get(lessonDO.getClassId());
    			if(lessonClassDO!=null){
    				lessonDO.setClassId(lessonClassDO.getName());
    			}else{
    				lessonDO.setClassId("");
    			}
			}
    	}
		int total = lessonService.count(query);
		PageUtils pageUtils = new PageUtils(lessonList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("wx:lesson:add")
	String add(){
	    return "wx/lesson/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("wx:lesson:edit")
	String edit(@PathVariable("id") String id,Model model){
		LessonDO lesson = lessonService.get(id);
		model.addAttribute("lesson", lesson);
		Map<String, Object> map = new HashMap<>();
		map.put("delFlag", "0");
		map.put("sort", "location");
		map.put("order", "asc");
		map.put("status", "0");
		List<LessonClassDO> list = lessonClassService.list(map);
		model.addAttribute("lessonClassList", list);
	    return "wx/lesson/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("wx:lesson:add")
	public R save( LessonDO lesson){
		if(lesson.getClassId()==null||"".equals(lesson.getClassId()==null)){
			return R.error("请选择分类");
		}
		lesson.setRecommend(1);
		lesson.setVideoFlag(1);
	    lesson.setId(UUIDUtils.getUUID());
	    lesson.setDelFlag(0);
	    lesson.setCaAt(new Date());
		if(lessonService.save(lesson)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wx:lesson:edit")
	public R update( LessonDO lesson){
		lesson.setUpAt(new Date());
		lessonService.update(lesson);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wx:lesson:remove")
	public R remove( String id){
		LessonDO lesson = lessonService.get(id);
		lesson.setDelFlag(1);
		lesson.setUpAt(new Date());
		if(lessonService.update(lesson)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("wx:lesson:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
	   if(ids.length>0){
			for (String string : ids) {
				LessonDO lesson = lessonService.get(string);
				lesson.setDelFlag(1);
				lesson.setUpAt(new Date());
				lessonService.update(lesson);
			}
		}
	
		return R.ok();
	}
	
	
	/***
	 * 2020年4月21日下午6:22:49
	 * return  R
	 * @author long
	 * 获取所有课程分类
	 */
	@ResponseBody
	@RequestMapping("/getClasses")
	public R getClasses() {
		List<Map<String,Object>> list = lessonClassService.queryAllForIdAndName(new HashMap<String,Object>());
		return R.ok().put("data", list);
	}
	
	//修改推荐课程页面
		@GetMapping("/resetRecommend/{id}")
		@RequiresPermissions("wx:lesson:edit")
		String resetRecommend(@PathVariable("id") String id,Model model){
			//获取视频信息
			LessonDO lessonDO = lessonService.get(id);
			model.addAttribute("lessonDO", lessonDO);
			return "wx/lesson/reset_recommend";
		}

		/**
		 * 修改推荐视频
		 *
		 * @param
		 * @return
		 */
		@ResponseBody
		@PostMapping("/updateRecommend")
		public R updateRecommend(LessonDO lessonDO) {
			lessonService.update(lessonDO);
			return R.ok();
		}
	
}
